package net;

import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

public class Queues<E> implements Queue<E>
{
  public static final int  CAPACITY = 1000;  // default queue capacity
  protected int  capacity;  // current queue capacity
  protected int  front;  // index of the first element in queue
  protected int  next;  // index of the next available array cell in queue
  protected E[]  genArray;  // generic array used to implement the queue
  
  public Queues()
  {
    this(CAPACITY);
  }
  
  public Queues(int cap)
  {
    capacity = cap + 1;
    genArray = (E[]) new Object[capacity];    
    front = next = 0;
  }
 
  public void enqueue(E element)
  {    
    if(size() == capacity - 1)
    {
      int  newSize = capacity * 2;
      E[] newArray = (E[]) new Object[newSize];
      
      for(int i = 0, j = front; i < size(); i++)
      {        
        newArray[i] = genArray[j];
        
        j = (++j)%capacity;
      }
      
      // in case of wrap-around, assign front and next properly
      front = 0;
      next = capacity - 1;
      
      // set old array pointer to new array
      capacity = newSize;
      genArray = newArray;
    }
    
    // insert element, increment next pointer (wrap-around supported)
    genArray[next] = element;
    next = (++next)%capacity;
  }

  
  public E dequeue()
  {
    E element;
    
    if(isEmpty())
		try {
			throw new Exception("Queue is empty.");
		} catch (Exception e) {
			e.printStackTrace();
		}
    
    // remove element, null and increment front pointer (wrap-around supported)
    element = genArray[front];
    genArray[front] = null;
    front = (++front)%capacity;
    
    return element;
  }
  
 
  public E front() throws Exception
  {
    if(isEmpty())
      throw new Exception("Queue is empty.");
    
    return genArray[front];
  }
  
 
  public int size()
  {
    // return the size, wrap-around supported
    return(capacity - front + next)%capacity;
  }
  
  
  public boolean isEmpty()
  {
    return(front == next);
  }
  
  public static Object[] nullArray(Object[] array)
  {
    for(int i = 0; i < array.length; i++)
    {
      array[i] = null;
    }
    
    return array;
  }

@Override
public boolean addAll(Collection<? extends E> arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public void clear() {
	// TODO Auto-generated method stub
	
}

@Override
public boolean contains(Object arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean containsAll(Collection<?> arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public Iterator<E> iterator() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean remove(Object arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean removeAll(Collection<?> arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public boolean retainAll(Collection<?> arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public Object[] toArray() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public <T> T[] toArray(T[] arg0) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean add(E arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public E element() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public boolean offer(E arg0) {
	// TODO Auto-generated method stub
	return false;
}

@Override
public E peek(){
	E element;
    
    if(isEmpty())
		try {
			throw new Exception("Queue is empty.");
		} catch (Exception e) {
			e.printStackTrace();
		}
    
    // remove element, null and increment front pointer (wrap-around supported)
    element = genArray[front];
    return element;
}

@Override
public E poll() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public E remove() {
	// TODO Auto-generated method stub
	return null;
}
  
  
}